/*DncIqBreQz6WZRX+v7OzgYtl1iEmq5jC4MuUyOKq4bc=*/
import { ajax, promptBox, toast } from 'nc-lightapp-front';
import { currentTypeAfterFormEvents } from '../../../../public/components/pubUtils/currentTypeAfterEvent';
import { autoAddLineKeys } from '../../../../public/components/pubUtils/billPubInfo';
import { checknoDisplayAfterEvent, checktypeAfterEvent,isbillrangeEdit } from '../../../../public/components/pubUtils/specialFieldAfterEvent.js';
import { formulamsgHint, renderData, headAfterEventRenderData, bodyAfterEventRenderData, errorDeal } from '../../../../public/components/afterEventPub/afterEventPubDeal';
import { getColvalues, getRowIds } from '../../../../public/components/pubUtils/billPubUtil';
import { moneyAndRateFields } from '../../../../public/components/pubUtils/specialFieldAfterEvent.js';
import { isExistsTopBill } from '../../../../public/components/pubUtils/billPubUtil.js';
import {afterEventFor36S3} from '../../../../public/components/pubUtils/arap436s3utils';
import { setAllocField } from '../../../../public/components/allocation.js';
import {refreshSmartCard} from '../../../../public/components/arapSmartCard.js';
export default function afterEvent(props, moduleId, key, value, changedrows, i, record, g) {
	if (changedrows instanceof Array) {
		if (changedrows[0].newvalue.value == changedrows[0].oldvalue.value) {
			return;
		}
	}
	let pagecode = this.getPagecode();
	let index = 0;
	if (moduleId == this.formId) {
		index = 0;
	} else if (moduleId == this.tableId) {
		index = i;
	}
	if(this.props.getUrlParam("src")=="36S3"){
		afterEventFor36S3(this,props, moduleId, key, value, changedrows, i, record, g);
		return;
	}
	refreshSmartCard({props,key,moduleId, attrcode:key, record});//更新智能区
	//表头编辑后事件
	if (moduleId == this.formId) {
		let data = null;
		switch (key) {
			case 'pk_org_v':
				if (value.value == null || value.value == '') {
					//清空财务组织
					if (this.props.getUrlParam('type') === 'transfer') {
						promptBox({
							color: 'warning',
							title: this.state.json['paybill-000018'] /* 国际化处理： 确认修改*/,
							content: this.state.json['paybill-000053'] /* 国际化处理： 来源于上游的单据不允许清空财务组织！*/,
							beSureBtnName: this.state.json['paybill-000004'] /* 国际化处理： 确定*/,
							noCancelBtn: true,
							beSureBtnClick: () => {
								this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
							},
							closeByClickBackDrop: false
						});
					} else {
						promptBox({
							color: 'warning',
							title: this.state.json['paybill-000018'] /* 国际化处理： 确认修改*/,
							content: this.state.json['paybill-000019'] /* 国际化处理： 确定​修改组织，这样会清空您录入的信息?*/,
							beSureBtnName: this.state.json['paybill-000004'] /* 国际化处理： 确定*/,
							cancelBtnName: this.state.json['paybill-000005'] /* 国际化处理： 取消*/,
							beSureBtnClick: () => {
								this.props.form.setFormItemsValue("allocobjrule",  { "allocobjrule": { value: '', display: '' } });
								this.props.form.EmptyAllFormValue(this.formId);
								this.props.cardTable.setTableData(this.tableId, { rows: [] });
								this.initAdd(true);
							},
							cancelBtnClick: () => {
								this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
							},
							closeByClickBackDrop: false
						});
					}
				} else if (changedrows.value != null && changedrows.value != '') {
					//切换组织
					promptBox({
						color: 'warning',
						title: this.state.json['paybill-000018'] /* 国际化处理： 确认修改*/,
						content: this.state.json['paybill-000019'] /* 国际化处理： 确定​修改组织，这样会清空您录入的信息?*/,
						beSureBtnName: this.state.json['paybill-000004'] /* 国际化处理： 确定*/,
						cancelBtnName: this.state.json['paybill-000005'] /* 国际化处理： 取消*/,
						beSureBtnClick: () => {
							this.props.form.setFormItemsValue("allocobjrule",  { "allocobjrule": { value: '', display: '' } });
							ajax({
								url: '/nccloud/arap/paybill/cardheadafteredit.do',
								data: {
									pageId: pagecode,
									event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
									uiState: this.props.getUrlParam('status')
								},
								async: false,
								success: (res) => {
									//渲染数据
									renderData(this, res);
									//编辑公式提示
									formulamsgHint(this, res);
								},
								error: (res) => {
									errorDeal(this, res, changedrows, key);
								}
							});
						},
						cancelBtnClick: () => {
							this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
						},
						closeByClickBackDrop: false
					});
				} else {
					data = {
						pageId: pagecode,
						event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
						uiState: this.props.getUrlParam('status')
					}
					headFieldAfterRequest.call(this, data, key, changedrows)
				}
				break;
			case 'supplier_v':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org','pk_org_v', 'payaccount', 'recaccount','pk_currtype',  'supplier','supplier_v', 'buysellflag',
						'objtype', "direction", "pk_billtype", "top_billtype", "pk_payterm", "pk_psndoc", "pk_deptid", "pk_deptid_v",'pk_ratetype','ratedate','ordercubasdoc','ordercubasdoc_v'].concat(moneyAndRateFields)),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'payaccount':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org','pk_org_v', 'payaccount', 'pk_currtype', 'supplier', 'supplier_v', 'buysellflag',
						'objtype', "direction", "pk_billtype"].concat(moneyAndRateFields)),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_deptid_v':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org','pk_org_v', 'pk_deptid', 'pk_deptid_v', 'pk_currtype', 'supplier','supplier_v', 'taxtype', 'buysellflag', 'objtype', "direction",'pk_psndoc','pk_pcorg','pk_pcorg_v', 'costcenter', 'billdate']),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_psndoc':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org','pk_org_v', 'pk_psndoc', 'payaccount', 'recaccount', 'pk_deptid', 'pk_deptid_v', 'isrefused', 'prepay', 'isdiscount', 'objtype', 'direction', 'agentreceivelocal','buysellflag','pk_pcorg','pk_pcorg_v','costcenter']),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_currtype':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ["pk_org","pk_org_v","pk_group","pk_currtype","pk_billtype","billdate","rate","grouprate","globalrate",
					"buysellflag","taxprice","local_taxprice","taxrate","occupationmny","money_bal","local_money_bal",
					"globaldebit","globalnotax_de","globaltax_de","groupdebit","groupnotax_de", "recaccount",
					"grouptax_de","local_money_de","local_notax_de","local_tax_de","money_de","notax_de","quantity_de","direction","settlecurr","settlemoney","pk_ratetype","ratedate","top_billtype","settlemoney","settlecurr",'supplier','supplier_v']),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				//币种事件发送完要判断汇率
				//v2111 汇率控制发生变化，都走编辑前进行控制 currentTypeAfterFormEvents(this.formId, props, key);
				break;
			case 'subjcode':
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				this.props.form.setFormItemsValue(this.formId, { 'subjcode': { value: value.refpk, display: value.dispname } });
				break;
			case 'pk_ratetype':
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				if (value.value == null || value.value == '') {
					promptBox({
						color: 'warning',
						title: this.state.json['paybill-000058'] /* 国际化处理： 确认清空*/,
						content: this.state.json['paybill-000059'] /* 国际化处理： 确定​清空汇率类型，这样会清空金额相关的信息！*/,
						beSureBtnName: this.state.json['paybill-000004'] /* 国际化处理： 确定*/,
						cancelBtnName: this.state.json['paybill-000005'] /* 国际化处理： 取消*/,
						beSureBtnClick: () => {
							headFieldAfterRequest.call(this, data, key, changedrows)
						},
						cancelBtnClick: () => {
							this.props.form.setFormItemsValue(this.formId, { pk_ratetype: changedrows });
						},
						closeByClickBackDrop: false
					});
				} else {
					headFieldAfterRequest.call(this, data, key, changedrows)
				}
				break;
			case 'pk_periodrule':
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows);
				setAllocField(this);
				break;
			default:
				if(key=='startdate'&&value.value){
					value.value = value.value.substr(0, 10)+' 00:00:00';
					this.props.form.setFormItemsValue(this.formId, { key: value });
				}else if(key=='enddate'&&value.value){
					value.value = value.value.substr(0, 10)+' 23:59:59';
					this.props.form.setFormItemsValue(this.formId, { key: value });
				}
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
		}
	}

	//表体编辑后事件
	if (moduleId == this.tableId) {
		//票据类型
		if (key == 'checktype') {
			checktypeAfterEvent(this.props, this.tableId, key, value, i);
		}
		if (key == 'direct_checkno') {
			let checkno = this.props.cardTable.getValByKeyAndIndex(this.tableId, i, "checkno");
			let checknovalue = checkno?checkno.value:null;
			if(value=="N"&&isbillrangeEdit(checknovalue,value)){
				this.props.cardTable.setEditableByIndex(this.tableId, i, ['billrangestart','billrangeend'], true);
			}else{
				this.props.cardTable.setEditableByIndex(this.tableId, i, ['billrangestart','billrangeend'], false);
			}
		}
		//非元数据字段，票据号
		if (key == 'checkno_display') {
			checknoDisplayAfterEvent(this.props, this.tableId, key, value, i);
			key = 'checkno';
			let billtypecode = value.values.pk_billtypecode.value;
			let subcontractcirculation = value.values.subcontractcirculation.value;
			if(billtypecode=="36H2"){
				this.isEditbillrangge = false;
			}else if(billtypecode=="36H1"){
				if(subcontractcirculation=="Y"){
					this.isEditbillrangge = true;
				}else{
					this.isEditbillrangge = false;
				}
			}
		}
		//科目字段特殊拼接1121\应收票据
		if(key == "subjcode"){
			// this.props.cardTable.setValByKeyAndIndex(this.tableId, i, 'subjcode', { value: value.refpk, display: value.dispname })
		}
		if(key=='startdate'&&value){
			value = value.substr(0, 10)+' 00:00:00';
			this.props.cardTable.setValByKeyAndIndex(this.tableId, i , key , {value: value});
		}else if(key=='enddate'&&value){
			value = value.substr(0, 10)+' 23:59:59';
			this.props.cardTable.setValByKeyAndIndex(this.tableId, i , key , {value: value});
		}
		ajax({
			url: '/nccloud/arap/paybill/cardbodyafteredit.do',
			data: {
				rowindex: 0,
				editindex: index,
				pageId: pagecode,
				changedrows: changedrows,
				tableId:this.tableId,
				body: props.cardTable.getDataByIndex(this.tableId, index),
				formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
				uiState: this.props.getUrlParam('status')
			},
			loading:false,
			async: false,
			success: (res) => {
				//渲染数据
				bodyAfterEventRenderData(this, res, index);
				if (this.props.getUrlParam('type') == 'transfer') {
					this.synTransferData();
				}
				//表体改变表头税率编辑性
				if (i == 0 && key == 'pk_currtype') {
					//v2111 汇率控制发生变化，都走编辑前进行控制 currentTypeAfterFormEvents(this.formId, props, 'pk_currtype');
				}
				//编辑公式提示
				formulamsgHint(this, res);
			},
			error: (res) => {
				let str = res.message;
				if (str.substring(0,16) == 'convertException') {
					promptBox({
						color: 'warning',
						title: this.state.json['paybill-000000'] /* 国际化处理： 折算误差*/,
						content: str.substring(16, str.length),
						closeByClickBackDrop: false,
						beSureBtnClick: () => {
							afterTableEvent(this, props, i, pagecode, moduleId, key, changedrows, 'sure', index,value);
						},
						cancelBtnClick: () => {
							afterTableEvent(this, props, i, pagecode, moduleId, key, changedrows, 'cancel', index,value);
						}
					});
				} else {
					if (key == 'startdate' || key == 'enddate'||key=='firstallocdate') {
						this.props.cardTable.setValByKeyAndRowId(this.tableId, changedrows[0].rowid, key, { value: changedrows[0].oldvalue.value, display: changedrows[0].oldvalue.display });
					} else {
						this.props.cardTable.setValByKeyAndRowId(this.tableId, i, key, changedrows);
					}
					toast({ color: 'danger', content: str});;
				}
			}
		});

		//自动增行
		autoAddline.call(this, moduleId, pagecode, key, i)
	}
}

function afterTableEvent(that, props, i, pagecode, moduleId, key, changedrows, isCalculateConvert, index,value) {

	ajax({
		url: '/nccloud/arap/paybill/cardbodyafteredit.do',
		data: {
			rowindex: 0,
			editindex: index,
			pageId: pagecode,
			changedrows: changedrows,
			tableId:that.tableId,
			body: props.cardTable.getDataByIndex(that.tableId, index),
			formEvent: props.createFormAfterEventData(pagecode, that.formId, that.tableId, key, value),
			uiState: that.props.getUrlParam('status'),
			isCalculateConvert: isCalculateConvert
		},
		async: false,
		success: (res) => {
			//渲染数据
			bodyAfterEventRenderData(that, res);
			if (that.props.getUrlParam('type') == 'transfer') {
				that.synTransferData();
			}
			//表体改变表头税率编辑性
			if (i == 0 && key == 'pk_currtype') {
				//v2111 汇率控制发生变化，都走编辑前进行控制 currentTypeAfterFormEvents(that.formId, props, 'pk_currtype');
			}
			//编辑公式提示
			formulamsgHint(that, res);
		}
	});
}

export function headFieldAfterRequest(requestData, key, changedrows) {
	ajax({
		url: '/nccloud/arap/paybill/cardheadafteredit.do',
		data: requestData,
		async: false,
		success: (res) => {
			//渲染数据
			headAfterEventRenderData(this, res);
			//编辑公式提示
			formulamsgHint(this, res);

			if (key == 'pk_org_v') {
				let pk_org_v = this.props.form.getFormItemsValue(this.formId, 'pk_org_v').value;
				if (pk_org_v) {
					this.props.resMetaAfterPkorgEdit();
					this.state.buttonfalg = true;
				} else {
					this.state.buttonfalg = null;
				}
				this.toggleShow();
				this.props.cardTable.setColValue(this.tableId,'prepay', { display:this.prepay.display,value: this.prepay.value});
			}

			if (this.props.getUrlParam('type') == 'transfer') {
				this.synTransferData();
			}
		},
		error: (res) => {
			errorDeal(this, res, changedrows, key);
		}
	});
}

export function autoAddline(moduleId, pagecode, key, i) {
	//自动增行
	let allRowsNumber = this.props.cardTable.getNumberOfRows(this.tableId);
	if (moduleId == this.tableId && allRowsNumber == i + 1 && autoAddLineKeys.indexOf(key) != -1 && !isExistsTopBill(this)) {
		let data = this.props.createMasterChildData(pagecode, this.formId, this.tableId);
		//清空cardData的表体
		data.body[this.tableId].rows = [];
		ajax({
			url: '/nccloud/arap/paybillpub/addline.do',
			data: data,
			loading:false,
			async: false,
			success: (res) => {
				if (res.data) {
					if (res.data.head) {
						this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
					}
					if (res.data.body) {
						this.props.cardTable.addRow(this.tableId, allRowsNumber, res.data.body[this.tableId].rows[0].values, false);
						this.props.cardTable.setValByKeyAndIndex(this.tableId, allRowsNumber, 'prepay', { display:this.prepay.display,value:this.prepay.value});
					}
				}
			}
		});
	}
}
/*DncIqBreQz6WZRX+v7OzgYtl1iEmq5jC4MuUyOKq4bc=*/